package com.net.loaderManage.item
{
    import com.libs.json.JSON;
    
    import flash.events.*;
    import flash.net.*;
    import flash.xml.*;
    
    import mx.rpc.xml.*;

    public class UrlLoaderItem extends Object
    {
        private var tryNum:int = 3;
        private var callback:Function;
        private var callbackParams:Object;
        private var url:String;

        public function UrlLoaderItem(param1:String, param2:Function, param3:Object)
        {
            this.url = param1;
            this.tryNum = 3;
            var _loc_4:* = new URLLoader();
            var _loc_5:* = new URLRequest(param1);
            _loc_4.addEventListener(Event.COMPLETE, completeHandle2);
            _loc_4.addEventListener(IOErrorEvent.IO_ERROR, errorHandle);
            _loc_4.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandle);
            _loc_4.load(_loc_5);
            this.callback = param2;
            this.callbackParams = param3;
            return;
        }// end function

        private function errorHandle(event:IOErrorEvent) : void
        {
            var _loc_3:UrlLoaderItem = this;
            var _loc_4:* = this.tryNum - 1;
            _loc_3.tryNum = _loc_4;
            var _loc_2:* = event.target as URLLoader;
            if (this.tryNum > 0)
            {
                _loc_2.load(new URLRequest(this.url));
            }
            else
            {
                _loc_2.removeEventListener(Event.COMPLETE, completeHandle2);
                _loc_2.removeEventListener(IOErrorEvent.IO_ERROR, errorHandle);
                _loc_2.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandle);
                if (callback != null)
                {
                    this.callback("error", null, this.callbackParams);
                }
                this.callback = null;
            }
            return;
        }// end function

        private function completeHandle(event:Event) : void
        {
            var decode:SimpleXMLDecoder;
            var result:Object;
            var event:Event = event;
            var loader:URLLoader = event.target as URLLoader;
            loader.removeEventListener(Event.COMPLETE, completeHandle2);
            loader.removeEventListener(IOErrorEvent.IO_ERROR, errorHandle);
            loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandle);
            if (callback != null)
            {
                try
                {
                    decode = new SimpleXMLDecoder(true);
                    result = decode.decodeXML(new XMLDocument(loader.data));
                    this.callback("success", result, this.callbackParams);
                }
                catch (e:Error)
                {
                    trace(e);
                }
            }
            this.callback = null;
            return;
        }// end function
		
		private function completeHandle2(event:Event) : void
		{
			var result:Object;
			var event:Event = event;
			var loader:URLLoader = event.target as URLLoader;
			loader.removeEventListener(Event.COMPLETE, completeHandle);
			loader.removeEventListener(IOErrorEvent.IO_ERROR, errorHandle);
			loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandle);
			if (callback != null)
			{
				try
				{
					result = JSON.decode(loader.data);
					this.callback("success", result, this.callbackParams);
				}
				catch (e:Error)
				{
					trace(e);
				}
			}
			this.callback = null;
			return;
		}// end function

    }
}
